<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 17:42
 */

namespace HiCommon\Service;

use HiCommon\Repository\ShopCheckpointServiceSpecsRepository;

class ShopCheckpointServiceSpecsService
{

//    /**
//     * 获取所有权限
//     *
//     * @return array
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function get_all(){
//        try {
//            $arr_option = ['order'=>['key'=>'id','value'=>'asc']];
//            $data = ShopCheckpointServiceSpecsRepository::get_all($arr_option)->toarray();
//            return  hello_success('成功',$data);
//        } catch (\Exception $exception) {
//            return hello_error('失败',[],$exception->getMessage());
//        }
//    }

    /**
     * 获取树形结构数据-条件门店id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_tree_by_shopid($arr_where)
    {
        try {

            $arr_option = [
              'order'=>['key'=>'id','value'=>'desc'],
              'where'=>$arr_where
            ];
            $data = ShopCheckpointServiceSpecsRepository::get_all_not_delete($arr_option)->toarray();
            $data = make_tree($data);//转换成树形数组
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 获取分页数据-条件门店id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_shopid($arr_where)
    {
        $arr_option = [
          'order'=>['key'=>'id','value'=>'desc'],
          'where'=>$arr_where
        ];
        $data = ShopCheckpointServiceSpecsRepository::get_all_not_delete($arr_option)->toarray();
        $data = make_tree($data);//转换成树形数组
     /*   $data = tree_to_one_tree($data);//树形数组转换成一维伪树形数组
        $arr_data = [];
        foreach ($data as $value)
        {
            $value['pre_ico'] = str_repeat('&nbsp;&nbsp;',$value['level']);
            $arr_data[] = $value;
        }*/
        return  hello_success('成功',$data);

    }

    /**
     * 获取分页数据-条件门店id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_shopid_for_dikaer($arr_where)
    {
        $arr_option = [
          'order'=>['key'=>'id','value'=>'desc'],
          'where'=>$arr_where
        ];
        $data = ShopCheckpointServiceSpecsRepository::get_all_not_delete($arr_option)->toarray();
        $data = make_tree($data);//转换成树形数组

        $arr_product = [];
        $arr_product_price = [];
        if(!empty($data)) {

            foreach ($data as $value) {
                $arr_child = [];
                if (!empty($value['_child'])) {
                    foreach ($value['_child'] as $vv) {
                        $arr_child[$vv['id']]         = ['id' => $vv['id'], 'name' => $vv['name'], 'price' => $vv['price']];
                        $arr_product_price[$vv['id']] = $vv['price'];
                    }
                }
                $arr_product[] = $arr_child;
            }
            $arr_product = combine_cartesian($arr_product);
        }
        $arr_data = [];
        if(!empty($arr_product)) {

            foreach ($arr_product as $value) {
                $s_product_id    = '';
                $s_product_name  = '';
                $s_product_price = 0;
                foreach ($value as $vv) {
                    if ($s_product_id === '') {
                        $s_product_id   = $vv['id'];
                        $s_product_name = $vv['name'];
                    }
                    else {
                        $s_product_id   .= ':' . $vv['id'];
                        $s_product_name .= '，' . $vv['name'];
                    }
                    $s_product_price += $vv['price'];
                }
                $arr_data[] = [
                  'ids'   => $s_product_id,
                  'name'  => $s_product_name,
                  'price' => $s_product_price,
                ];
            }
        }
//        dd($arr_data);
        /*   $data = tree_to_one_tree($data);//树形数组转换成一维伪树形数组
           $arr_data = [];
           foreach ($data as $value)
           {
               $value['pre_ico'] = str_repeat('&nbsp;&nbsp;',$value['level']);
               $arr_data[] = $value;
           }*/
        return  hello_success('成功',$arr_data);

    }
    
    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        try {

            $arr_option = ['order'=>['key'=>'id','value'=>'desc']];
            $data = ShopCheckpointServiceSpecsRepository::get_list($arr_option);
            $data = list_page_data($data);
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        filter_input_data($arr_input);

        //添加
        $res = ShopCheckpointServiceSpecsRepository::add_one($arr_input)->toarray();
        if(empty($res)){
            return hello_error('添加失败');
        }

        return  hello_success('成功',$res);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        $n_id = (int) $n_id;
        if($n_id <= 0) {
            hello_error('数据ID不能为空');
        }

        //查询
        $res = ShopCheckpointServiceSpecsRepository::get_one($n_id)->toarray();
        if(empty($res)){
            return hello_error('查询失败');
        }

        return  hello_success('查询成功',$res);
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        filter_input_data($arr_input);
        $arr_input['id'] = (int)$arr_input['id'];
        if ($arr_input['id'] <= 0) {
            return hello_error('数据id不能为空');
        }

        $res = ShopCheckpointServiceSpecsRepository::edit_one($arr_input);
        if ($res === false) {
            return hello_error('操作失败');
        }
        return hello_success('成功');
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input){
        $arr_input['id'] = (int)$arr_input['id'];
        if ($arr_input['id'] <= 0) {
            return hello_error('数据id不能为空');
        }

        $res = ShopCheckpointServiceSpecsRepository::edit_one($arr_input);
        if ($res === false) {
            return hello_error('操作失败');
        }
        return hello_success('成功');
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        if ($n_id <= 0) {
            return hello_error('数据id不能为空');
        }

        $res = ShopCheckpointServiceSpecsRepository::delete_one_guige($n_id);
        if($res === false){
            return hello_error('操作失败');
        }
        return  hello_success('成功');
    }
}